package nac.firefight;

//~--- non-JDK imports --------------------------------------------------------

import net.phys2d.raw.CollisionEvent;
import net.phys2d.raw.CollisionListener;
import net.phys2d.raw.shapes.Box;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.state.StateBasedGame;

public class GamePlayState extends AbstractGameState implements CollisionListener {
    private int            direction = 0;
    private Sprite         plane     = null;
    private GameWorld      gameWorld;
    private AnimatedSprite rpg;

    public GamePlayState(int id, FireFightGame ffg) {
        super(id, ffg);
    }

    public void init(GameContainer gc, StateBasedGame sb) throws SlickException {
        ResourceMap rm = game.getResourceMap();

        plane = new Sprite(rm.getImage("plane.png"), "plane", new Box(100.0f, 100.0f), 50.0f);
        plane.setPosition(20, 20);

//      ++++++++++++++++++++++++++++++++++++++++++++++++
        SpriteSheet sheet = new SpriteSheet("Chara1.png", 48, 64);
        Animation   up    = new Animation();
        Animation   right = new Animation();
        Animation   down  = new Animation();
        Animation   left  = new Animation();

        for (int i = 0; i < 2; i++) {
            up.addFrame(sheet.getSprite(i, 0), 150);
        }

        for (int i = 0; i < 2; i++) {
            right.addFrame(sheet.getSprite(i, 1), 150);
        }

        for (int i = 0; i < 2; i++) {
            down.addFrame(sheet.getSprite(i, 2), 150);
        }

        for (int i = 0; i < 2; i++) {
            left.addFrame(sheet.getSprite(i, 3), 150);
        }

        Animation[] animations = { up, right, down, left };

        rpg = new AnimatedSprite(animations, "rpg", new Box(40f, 65f), 50f);
        rpg.setPosition(300, 300);

//      ++++++++++++++++++++++++++++++++++++++++++++++++++++
        gameWorld = new GameWorld(rm.getTiledMap("map.tmx"));
        gameWorld.addSprite(rpg);    // ++++++++++
        gameWorld.addSprite(plane);
        gameWorld.addListener(this);
    }

    public void render(GameContainer gc, StateBasedGame sb, Graphics g) throws SlickException {
        gameWorld.render(g, rpg);
            
    }

    public void update(GameContainer gc, StateBasedGame sb, int delta) throws SlickException {
        Input input = gc.getInput();

        if (input.isKeyDown(Input.KEY_A)) {
            plane.rotate(-0.009f, delta);
        }

        if (input.isKeyDown(Input.KEY_D)) {
            plane.rotate(0.009f, delta);
        }

        if (input.isKeyDown(Input.KEY_W)) {
            plane.moveForward(0.4f, delta);
        }

        if (input.isKeyDown(Input.KEY_Q)) {
            plane.flankLeft(0.4f, delta);
        }

        if (input.isKeyDown(Input.KEY_E)) {
            plane.flankRight(0.4f, delta);
        }

        if (input.isKeyDown(Input.KEY_UP)) {
            rpg.moveForward(0.2f, delta);
            rpg.setDrawIndex(0);
        }

        if (input.isKeyDown(Input.KEY_RIGHT)) {
            rpg.flankRight(0.2f, delta);
            rpg.setDrawIndex(1);
        }

        if (input.isKeyDown(Input.KEY_DOWN)) {
            rpg.moveRear(0.2f, delta);
            rpg.setDrawIndex(2);
        }

        if (input.isKeyDown(Input.KEY_LEFT)) {
            rpg.flankLeft(0.2f, delta);
            rpg.setDrawIndex(3);
        }

        gameWorld.update();
    }

    public void collisionOccured(CollisionEvent event) {
        System.out.println("Hello");
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
